"""
消息队列接口定义
===============
定义消息队列和事件总线的抽象接口，支持RabbitMQ、Kafka、Redis Pub/Sub等
"""

from abc import ABC, abstractmethod
from typing import Any, Optional, Dict, List, Callable, Awaitable
from datetime import datetime


class MessageInterface(ABC):
    """消息接口"""
    
    @abstractmethod
    def get_id(self) -> str:
        """获取消息ID"""
        pass
    
    @abstractmethod
    def get_body(self) -> Any:
        """获取消息体"""
        pass
    
    @abstractmethod
    def get_headers(self) -> Dict[str, Any]:
        """获取消息头"""
        pass
    
    @abstractmethod
    def get_routing_key(self) -> str:
        """获取路由键"""
        pass
    
    @abstractmethod
    def get_timestamp(self) -> datetime:
        """获取时间戳"""
        pass
    
    @abstractmethod
    async def ack(self) -> bool:
        """确认消息"""
        pass
    
    @abstractmethod
    async def nack(self, requeue: bool = True) -> bool:
        """拒绝消息"""
        pass


class MessagingInterface(ABC):
    """消息队列接口定义"""
    
    @abstractmethod
    async def connect(self) -> bool:
        """建立连接"""
        pass
    
    @abstractmethod
    async def disconnect(self) -> bool:
        """断开连接"""
        pass
    
    @abstractmethod
    async def publish(self, queue: str, message: Any, routing_key: str = None, 
                     headers: Dict[str, Any] = None) -> bool:
        """发布消息"""
        pass
    
    @abstractmethod
    async def subscribe(self, queue: str, callback: Callable[[MessageInterface], Awaitable[None]],
                       auto_ack: bool = False) -> bool:
        """订阅消息"""
        pass
    
    @abstractmethod
    async def unsubscribe(self, queue: str) -> bool:
        """取消订阅"""
        pass
    
    @abstractmethod
    async def create_queue(self, queue: str, durable: bool = True, 
                          exclusive: bool = False, auto_delete: bool = False) -> bool:
        """创建队列"""
        pass
    
    @abstractmethod
    async def delete_queue(self, queue: str) -> bool:
        """删除队列"""
        pass
    
    @abstractmethod
    async def queue_exists(self, queue: str) -> bool:
        """检查队列是否存在"""
        pass
    
    @abstractmethod
    async def get_queue_size(self, queue: str) -> int:
        """获取队列大小"""
        pass
    
    @abstractmethod
    async def purge_queue(self, queue: str) -> int:
        """清空队列，返回清理的消息数量"""
        pass


class EventBusInterface(ABC):
    """事件总线接口"""
    
    @abstractmethod
    async def publish_event(self, event_type: str, data: Any, 
                           metadata: Dict[str, Any] = None) -> bool:
        """发布事件"""
        pass
    
    @abstractmethod
    async def subscribe_event(self, event_type: str, 
                             handler: Callable[[str, Any, Dict[str, Any]], Awaitable[None]]) -> bool:
        """订阅事件"""
        pass
    
    @abstractmethod
    async def unsubscribe_event(self, event_type: str, handler_id: str) -> bool:
        """取消事件订阅"""
        pass
    
    @abstractmethod
    async def get_event_history(self, event_type: str, limit: int = 100) -> List[Dict[str, Any]]:
        """获取事件历史"""
        pass
